//
//  UserHelper.m
//  O2O
//
//  Created by Leafc on 13-10-24.
//  Copyright (c) 2013年 leafc. All rights reserved.
//

#import "UserHelper.h"
#import "MD5Helper.h"
#import "AFNetworking.h"

#define HTTP @"http://"
#define HTTPS @"https://"
#define REQUEST_URL @"wi.vipshop.com/public_api/gw.php"
#define HTTP_LOGIN_URL [NSString stringWithFormat:@"%@%@", HTTP, REQUEST_URL]
#define HTTPS_LOGIN_URL [NSString stringWithFormat:@"%@%@", HTTPS, REQUEST_URL]

#define API_KEY @"a26cc2dc17ed0b4f962e5c7b8c4c164b"
#define API_SECRET @"51b42ff4465941e107a84f3d24580d82"
#define LOGIN_SERVICE @"vipshop.user.base.login"
#define REGISTER_SERVICE @"vipshop.user.base.add"



@implementation UserHelper

+ (NSString *)dataSignature:(NSDictionary *)dict
{
    NSArray *allKeys = [dict allKeys];
    NSArray *sortedKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
    NSMutableString *source = [[NSMutableString alloc] init];
    for(id key in sortedKeys) {
        id object = [dict objectForKey:key];
        [source appendString:object];
    }
    [source appendString:API_SECRET];
    
    return [MD5Helper md5HexDigest:source];
}

+ (void)POSTToServiceWithParams:(NSDictionary *)dict
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:dict];
    [params setObject:API_KEY forKey:@"api_key"];
    [params setObject:@"json" forKey:@"format"];
    
    NSString *dataSignature = [UserHelper dataSignature:params];
    [params setObject:dataSignature forKey:@"api_sign"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager POST:HTTP_LOGIN_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

+ (void)signIn:(NSString *)userName password:(NSString *)password
{
    NSString *passwordMd5 = [MD5Helper md5HexDigest:password];
    NSDictionary *params = @{@"service":LOGIN_SERVICE,
                             @"fields":@"user_token,user_secret,user_blacklist",
                             @"login_id":userName,
                             @"password":passwordMd5};
    [self POSTToServiceWithParams:params];
}

+ (void)signUp:(NSString *)userName password:(NSString *)password
{
    NSString *passwordMd5 = [MD5Helper md5HexDigest:password];
    NSDictionary *params = @{@"service":REGISTER_SERVICE,
                             @"fields":@"user_token,user_secret",
                             @"login_type":@"email",
                             @"login_id":userName,
                             @"password":passwordMd5,
                             @"use_name":userName,
                             @"gender":passwordMd5,
                             @"birthday":passwordMd5,
                             @"inviter":userName,
                             @"verify_token":passwordMd5};
    [self POSTToServiceWithParams:params];
}

@end
